import { Controller, Get, Redirect, Render, Post, Body } from '@nestjs/common';
import { UpdateSettingDto } from 'src/shared/dto/setting.dto';
import { SettingService } from 'src/shared/services/setting.service';

@Controller('admin/settings')
export class SettingController {
  constructor(private readonly settingService: SettingService) {}
  @Get()
  @Render('settings')
  async getSettings() {
    let settings: any = await this.settingService.findFirst();
    if (!settings) {
      settings = await this.settingService.create({
        siteName: '默认网站',
        siteDescription: '默认网站描述',
        contactEmail: '联系人邮箱',
      });
    }
    settings = settings.toObject();
    // console.log('settings', settings);
    return {
      settings,
    };
  }

  @Post()
  @Redirect('/admin')
  async updateSettings(@Body() updateSettingDto: UpdateSettingDto) {
    await this.settingService.update(updateSettingDto.id, updateSettingDto);
  }
}
